package games.ninJump.ui.effects
{
	import core.Global;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import interfaces.IAction;
	
	import utils.MathTools;
	import utils.Particle;
	import utils.ParticlePreExisting;
	
	public class ExplodeEffect implements IAction
	{
		private var m_start:Boolean;
		private var particles:Array;
		private var m_target:DisplayObjectContainer;
		
		private var m_now:Number;
		private var m_total:Number = 500;
		private var m_speed:Number = 20;
		
		public function ExplodeEffect(mc:DisplayObjectContainer)
		{
			particles = new Array();
			m_target = mc;
			m_target.addEventListener(MouseEvent.CLICK,onClickExplodeClip);
			
		}
		
		public function set start(b:Boolean):void
		{
			m_start = b;
			if(b)
			{
				m_now = getTimer();
			}else
			{
				for(var i:Number = 0;i<particles.length;i++)
				{
					
					//... and update each one
					var particle:Particle = particles[i];
					particle.destroy();
					
					
				}
				m_target.removeEventListener(MouseEvent.CLICK,onClickExplodeClip);
				particles = null;
				if(m_target.parent)m_target.parent.removeChild(m_target);
				m_target = null;
			}
		}
		
		public function get start():Boolean
		{
			return m_start;
		}
		
		public function update(e:Event=null):void
		{
			if(!m_start)return;
			if(getTimer() - m_now >= m_total)
			{
				start = false;
				return;
			}
			updateParticles();
		}
	
		
		private function onClickExplodeClip(e:MouseEvent):void
		{
			
			trace("**************************click fireball");
			// and find each clip within our clip. 
			for(var i:Number=0; i<m_target.numChildren; i++)
				
			{
				// and make a particle out of it!
				makeExplodeParticle(m_target.getChildAt(i));
				
			}
			
			Global.addAction(this);
			
		}
		
		
		private function makeExplodeParticle(clip:DisplayObject):void
		{
			var particle : ParticlePreExisting = new ParticlePreExisting(clip);
			
			var angle:Number = Math.PI * 2 * Math.random();
			var speed:Array = MathTools.getSpeedByVector(m_speed,angle);
			particle.setVel(speed[0],speed[1]);
			
			//particle.gravity = 1.5;
			particle.drag = 0.98;
		 	//particle.spin = MathTools. randRange(-20,20);
			particle.directionRotate = true;
			particle.setScale(Math.random() * 1);
			particles.push(particle);
			
		}
		
		
		private function updateParticles():void
		{
			
			var particle:Particle;
			
			// if there are more than 40 particles delete the first one in the array... 
			while(particles.length>40)
			{
				particle = Particle(particles.shift());
				particle.destroy();
				
			}
			
			// go through the array of particles... 
			for(var i:Number = 0;i<particles.length;i++)
			{
				
				//... and update each one
				particle = particles[i];
				particle.update();
				
				
			}
			
			
		}
		
	
		
	}
}